package com.example.teaknower.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.teaknower.R;
import com.example.teaknower.model.Tea_article;

import java.util.List;
import java.util.zip.Inflater;

public class TeaAdapter extends BaseAdapter {

    private List<Tea_article> teaList;
    private Context context;
    private LayoutInflater inflater;

    public TeaAdapter(List<Tea_article> teaList, Context context) {
        this.teaList = teaList;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return teaList.size();
    }

    @Override
    public Object getItem(int position) {
        return teaList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder viewHolder;
       if(convertView==null){
           viewHolder = new ViewHolder();
           convertView = inflater.inflate(R.layout.artitle_item,parent,false);
           viewHolder.imageView =(ImageView)convertView.findViewById(R.id.article_list_image);
           viewHolder.time=(TextView)convertView.findViewById(R.id.article_list_time);
           viewHolder.title=(TextView)convertView.findViewById(R.id.article_list_title);
           convertView.setTag(viewHolder);
       }else {
           viewHolder=(ViewHolder)convertView.getTag();
       }

       Tea_article tea = teaList.get(position);
       viewHolder.title.setText(tea.getTitle());
       viewHolder.time.setText(tea.getTime());
       String url = tea.getImageUrl();
       if(!TextUtils.isEmpty(url)){
           Glide.with(context).load(url).into(viewHolder.imageView);
       }
        return convertView;
    }

    class ViewHolder{
        private ImageView imageView;
        private TextView title;
        private TextView time;
    }
}
